package com.itheima.stock.config;

import com.itheima.stock.config.vo.TaskThreadPoolInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @author by itheima
 * @Date 2022/3/21
 * @Description 配置线程池bean对象
 */
@Configuration
public class  TaskExecutePoolConfig {

    @Autowired
    private TaskThreadPoolInfo taskThreadPoolInfo;

    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
        //1.构建线程池对象
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //2.设置参数
        //2.1 设置核心线程数
        executor.setCorePoolSize(taskThreadPoolInfo.getCorePoolSize());
        //2.2 设置最大线程数
        executor.setMaxPoolSize(taskThreadPoolInfo.getMaxPoolSize());
        //2.3 设置线程的最大存活时间
        executor.setKeepAliveSeconds(taskThreadPoolInfo.getKeepAliveSeconds());
        //2.4 设置任务队列
        executor.setQueueCapacity(taskThreadPoolInfo.getQueueCapacity());
        //2.5 设置线程池的任务拒绝策略
        //executor.setRejectedExecutionHandler(null);
        //2.6 设置核心线程对象超时回收
        executor.setAllowCoreThreadTimeOut(true);
        //3.参数初始化
        executor.initialize();
        //4.返回
        return executor;
    }


}
